package com.example.jegotrip.fragment.mine;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toolbar;

import com.example.jegotrip.R;
import com.example.jegotrip.base.BaseActivity;
import com.example.jegotrip.db.NoteDBHelper;
import com.example.jegotrip.db.UserDBHelper;
import com.example.jegotrip.entity.NoteInfo;
import com.example.jegotrip.entity.UserInfo;
import com.example.jegotrip.utils.CustomToast;

public class UpdatePwdActivity extends BaseActivity {

    private Toolbar toolbar;
    private EditText ev_input_new_password;
    private EditText ev_input_new_password_again;
    private Button btn_confirm_update_password;
    private UserInfo userInfo;

    @Override
    protected void initData() {
        userInfo = UserInfo.getUserInfo();
    }

    @Override
    protected void initViews() {
        toolbar = findViewById(R.id.toolBar);
        ev_input_new_password = findViewById(R.id.ev_input_new_password);
        ev_input_new_password_again = findViewById(R.id.ev_confirm_new_password);
        btn_confirm_update_password = findViewById(R.id.ev_update_password);
    }

    @Override
    protected int getLayoutResOId() {
        return R.layout.activity_update_password;
    }

    @Override
    protected void setListener() {
        toolbar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        btn_confirm_update_password.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String new_password = ev_input_new_password.getText().toString();
                String new_password_again = ev_input_new_password_again.getText().toString();
                if (TextUtils.isEmpty(new_password) || TextUtils.isEmpty(new_password_again)){
                    CustomToast.showCenterToast(UpdatePwdActivity.this,"请输入新密码~");
                } else {
                    if (!new_password.equals(new_password_again)){
                        CustomToast.showCenterToast(UpdatePwdActivity.this,"两次输入密码不一致");
                    } else {
                        int row = UserDBHelper.getInstance(UpdatePwdActivity.this).updatePassword(userInfo.getUsername(),new_password);

                        if (row > 0){
                            CustomToast.showCenterToast(UpdatePwdActivity.this,"修改成功~");
                        } else {
                            CustomToast.showCenterToast(UpdatePwdActivity.this,"修改失败~");

                        }
                    }
                }
            }
        });
    }
}
